package cn.wzptsoft.mall.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.wzptsoft.mall.aspect.AutoFull;
import cn.wzptsoft.mall.aspect.OperationType;
import cn.wzptsoft.mall.mapper.AddressMapper;
import cn.wzptsoft.mall.pojo.Address;
import cn.wzptsoft.mall.pojo.LoginUser;
import cn.wzptsoft.mall.pojo.vo.AddressInfoVO;
import cn.wzptsoft.mall.pojo.vo.ThemeVo;
import cn.wzptsoft.mall.service.IAddressService;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.v3.oas.annotations.servers.Server;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class IAddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements IAddressService {
    @Override
    public List<AddressInfoVO> getUserAddress() {
        UsernamePasswordAuthenticationToken authentication = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
        LoginUser user = (LoginUser)authentication.getPrincipal();
        int id=user.getUser().getId();
        List<Address> eq = lambdaQuery().eq(Address::getUserId, id).list();

        List<AddressInfoVO> list = eq.stream().map(s -> BeanUtil.copyProperties(s, AddressInfoVO.class)).toList();
        return list;
    }

    @Override
    @AutoFull(OperationType.INSERT)
    public void add(Address address) {
        address.setUserId(address.getUpdater());
        save(address);
    }
}
